package main

import (
	"fmt"
	"github.com/panjf2000/ants/v2"
	"runtime"
	"sync"
	"time"
)

var poolSize1 int

func init() {
	poolSize1 = runtime.NumCPU() * 2
}
func main() {
	start := time.Now()
	defer ants.Release()

	var wg sync.WaitGroup
	// 创建池
	p, _ := ants.NewPool(poolSize1)
	defer p.Release()

	results := make(chan int, 100)

	// 提交带返回值的任务
	for i := 0; i < 100; i++ {
		wg.Add(1)
		i := i //创建局部变量副本
		_ = p.Submit(func() {
			defer wg.Done()
			results <- i * 3
		})
	}

	// 等待所有任务完成
	wg.Wait()
	close(results)
	// 收集结果
	for res := range results {
		fmt.Println("result from channel ", res)
	}
	end := time.Now()
	fmt.Println("time cost: ", end.Sub(start))
}
